package com.pro.controller;

import com.pro.pojo.*;
import com.pro.service.JsJurisdictionService;
import com.pro.utils.FastDFSClientUtil;
import com.pro.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cyj
 * @date 2022.2.27$ 18.47$
 * @content :
 */
@RestController
@RequestMapping("/newJurisdiction")
public class JsJurisdictionController {
    @Autowired
    private JsJurisdictionService service;
    @Autowired
    private FastDFSClientUtil dfsClient;

    private String acatarUrl;
    /**
     * @author cyj
     * @date 2022.2.27$ 15.59$
     * @content :获取所有的部门名称
     */
    @RequestMapping("/getDeptName")
    public JsonData getDeptName() {
        List<JsDept> AlldeptList = service.getDeptName();
        return AlldeptList!=null? JsonData.buildSuccess(AlldeptList):JsonData.buildError("服务器错误");
    }

    /**
     * @author cyj
     * @date 2022.2.27$ 19.10$
     * @content :获取所有员工信息
     */
    @RequestMapping("/getUsers")
    public JsonData getJsUser(String account,String trueName,String deptName,String registerDate,Integer limit,Integer page) {
        account = !("").equals(account) && account != null ? "%" + account + "%" : "%%";
        trueName = !("").equals(trueName) && trueName != null ? "%" + trueName + "%" : "%%";
        deptName = !("").equals(deptName) && deptName != null ? "%" + deptName + "%" : "%%";
        registerDate = !("").equals(registerDate) && registerDate != null ? "%" + registerDate + "%" : "%%";
        List<JsUser> jsUsers = service.getUsers(account, trueName, deptName, registerDate, limit, page);
        Integer count = service.countUser();
        return jsUsers != null ? JsonData.buildSuccess(jsUsers,count) : JsonData.buildError("服务器错误");
    }
    /**
     * @author cyj
     * @date 2022.3.3$ 15.30$
     * @content :获取所有员工信息
     */
    @RequestMapping("/updateStatus")
    public JsonData updateStatus(JsUser jsUser){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        Date date = new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = sdf.format(date);
        jsUser.setEndDate(dateString);
        jsUser.setUnsealDate(dateString);
        JsClosure jsClosure = new JsClosure();
        Boolean flag = service.updateStatus(jsUser);
        Integer status = jsUser.getStatus();
        Integer enabled = jsUser.getEnabled();
        Boolean aBoolean1 = false;
        Boolean endFlag = false;

        if(status==1&&enabled==1){
            aBoolean1 = service.insertUnseal(jsUser.getUserId(),jsUser.getType(),dateString);
            if(flag&&aBoolean1){
                endFlag = true;
            }
        }else{
            jsClosure.setUserId(jsUser.getUserId());
            jsClosure.setType(3);
            jsClosure.setRemarks(jsUser.getRemark());
            jsClosure.setStartDatee(jsUser.getUnsealDate());
            jsClosure.setEndDatee(jsUser.getEndDate());
            jsClosure.setDays(36524);
            jsClosure.setRemarks("紧急封停");
            Boolean aBoolean2 = service.addUserClosure(jsClosure);
            if(flag&&aBoolean2!=false){
                endFlag=true;
            }
        }
        return endFlag != false ? JsonData.buildSuccess(endFlag) : JsonData.buildError("服务器错误");
    }
    /**
     * @author cyj
     * @date 2022.3.4$ 9.13$
     * @content :查询某个用户的详细信息
     */
    @RequestMapping("/getUserById")
    public JsonData getJsUserById(Integer id){
        JsUser jsUserById = service.getUserById(id);
        if(jsUserById!=null){
            acatarUrl=jsUserById.getAcatarUrl();
        }
        return jsUserById != null ? JsonData.buildSuccess(jsUserById) : JsonData.buildError("服务器错误");
    }
    /**
     * @author cyj
     * @date 2022.3.7$ 8.53$
     * @content :查询某个用户的详细信息
     */
    @RequestMapping("/addUserClosure")
    public JsonData addUserClosure(JsClosure jsClosure) {
        JsUser jsUser = new JsUser();
        jsUser.setUserId(jsClosure.getUserId());
        jsUser.setStatus(jsClosure.getStatus());
        jsUser.setEnabled(jsClosure.getEnabled());
        Boolean updateUserStatus = service.updateStatus(jsUser);
        Boolean addUserClosure = service.addUserClosure(jsClosure);
        Boolean flag = false;
        if(updateUserStatus!=false&&addUserClosure!=false){
            flag = true;
        }
        return flag != false ? JsonData.buildSuccess(flag) : JsonData.buildError("服务器错误");
    }
    /**
     * @author cyj
     * @date 2022.3.8$ 8.10$
     * @content :修改某个用户的信息
     */
    @RequestMapping("/updateUser")
    public JsonData updateUser(JsUser jsUser){
        String url=jsUser.getAcatarUrl();
        System.err.println(url);
        if(url!=null&&url!=""){
            Boolean flag = service.updateUser(jsUser);
            return flag != false ? JsonData.buildSuccess(flag) : JsonData.buildError("服务器错误");
        }
        jsUser.setAcatarUrl(acatarUrl);
        Boolean flag = service.updateUser(jsUser);
        return flag != false ? JsonData.buildSuccess(flag) : JsonData.buildError("服务器错误");
    }
    /**
     * @author cyj
     * @date 2022.3.8$ 13.43$
     * @content :查询用户所拥有的权限
     */
    @RequestMapping("/getUserPower")
    public List<JsUser> getUserPower(Integer id){
        List<JsUser> userPower = service.getUserPower(id);
        return userPower;
    }
    /**
     * @author cyj
     * @date 2022.3.9$ 8.54$
     * @content :添加用户所拥有的权限
     */
    @RequestMapping("/addUserGrade")
    public JsonData addUserGrade(JsUser user){
        Boolean flag = service.addUserGrade(user);
        return flag?JsonData.buildSuccess(flag):JsonData.buildError("服务器错误");
    }
    /**
     * @author cyj
     * @date 2022.3.9$ 9.27$
     * @content :添加用户所拥有的权限
     */
    @RequestMapping("/delUserGrade")
    public JsonData delUserGrade(JsUser user){
        Boolean flag = service.delUserGrade(user);
        return flag?JsonData.buildSuccess(flag):JsonData.buildError("服务器错误");
    }
    /**
     * @author cyj
     * @date 2022.3.9$ 14.10$
     * @content :添加一位用户
     */
    @RequestMapping("/addUser")
    public JsonData addUser(JsUser user){
        System.err.println(user);
        user.setPassword(MD5Util.md5(user.getPassword()));
        InetAddress address = null;
        String hostAddress= "";
        try{
            address = InetAddress.getLocalHost();
            hostAddress = address.getHostAddress();
        }catch (Exception e){
            e.printStackTrace();
        }
        user.setRegisterIp(hostAddress);
        Boolean flag = service.addUser(user);
        return flag?JsonData.buildSuccess(flag):JsonData.buildError("服务器错误");
    }

    /**
     * @author cyj
     * @date 2022.3.9$ 14.23$
     * @content :上传以为用户的头像
     */
    @RequestMapping("/uploadFile")
    public JsonData fdfsUpload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        String resAccessUrl;
        String fileUrl = null;
        try {
            fileUrl = dfsClient.uploadFile(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //路径
        resAccessUrl = dfsClient.getResAccessUrl(fileUrl);
        Map<String,Object> map = new HashMap<>();
        map.put("path",resAccessUrl);
        return map!=null?JsonData.buildSuccess(map):JsonData.buildError("服务端错误");
    }
    /**
     * @author cyj
     * @date 2022.3.10$ 8.56$
     * @content :解封日志计数
     */
    @RequestMapping("/getUnsealingLog")
    public JsonData getUnsealingLog(String account, String deptName, String unsealDate, Integer limit, Integer page) {
        List<JsUnseal> unseal = service.getUnsealingLog(account,deptName,unsealDate,limit,page);
        Integer count = service.countUnsealingLog();
        return unseal!=null?JsonData.buildSuccess(unseal,count):JsonData.buildError("暂无数据");
    }
    /**
     * @author cyj
     * @date 2022.3.10$ 14.36$
     * @content :获取部门信息
     */
    @RequestMapping("/getJsDeptList")
    public JsonData getJsDeptList(String deptName,String trueName,String createDate,String status,Integer limit,Integer page) {
        List<JsDept> unseal = service.getJsDeptList(deptName,trueName,createDate,status,limit,page);
        Integer count = service.countUnsealingLog();
        return unseal!=null?JsonData.buildSuccess(unseal,count):JsonData.buildError("暂无数据");
    }
    /**
     * @author cyj
     * @date 2022.3.10$ 14.49$
     * @content :对部门进行删除
     */
    @RequestMapping("/deleteDept")
    public JsonData deleteDept(Integer id) {
        System.err.println(id);
        Boolean flag = service.deleteDept(id);
        System.err.println(flag);
        return flag!=false?JsonData.buildSuccess(flag):JsonData.buildError("服务器错误");
    }
    /**
     * @author cyj
     * @date 2022.3.10$ 15.22$
     * @content :对部门进行删除
     */
    @RequestMapping("/getDeptById")
    public JsonData getDeptById(Integer id) {
        JsDept dept = service.getDeptById(id);
        return dept!=null?JsonData.buildSuccess(dept):JsonData.buildError("服务器错误");
    }
    /**
     * @author cyj
     * @date 2022.3.10$ 15.32$
     * @content :对部门进行删除
     */
    @RequestMapping("/updateDept")
    public JsonData updateDept(JsDept jsDept) {
        Boolean flag = service.updateDept(jsDept);
        return flag!=null?JsonData.buildSuccess(flag):JsonData.buildError("服务器错误");
    }
    /**
     * @author cyj
     * @date 2022.3.10$ 15.50$
     * @content :对部门进行修改
     */
    @RequestMapping("/updateDeptStatus")
    public JsonData updateDeptStatus(JsDept jsDept) {
        Boolean flag = service.updateDeptStatus(jsDept);
        return flag!=null?JsonData.buildSuccess(flag):JsonData.buildError("服务器错误");
    }
    /**
     * @author cyj
     * @date 2022.3.10$ 16.18$
     * @content :对部门进行修改
     */
    @RequestMapping("/addDept")
    public JsonData addDept(JsDept jsDept) {
        Boolean flag = service.addDept(jsDept);
        return flag!=null?JsonData.buildSuccess(flag):JsonData.buildError("服务器错误");
    }
}